<template>
  <div class="mail">
    <div class="header">
      <table>
        <tr>
          <td colspan="12" style="position: absolute; right: 30px">
            <el-button
              type="success"
              size="small"
              @click="sendMail()"
              icon="el-icon-plus"
            >
              新建邮件
            </el-button>
          </td>
        </tr>
      </table>
    </div>
    <div class="content">
      <el-main style="margin-top: 60px" v-loading="loading">
        <el-table
          class="table"
          border
          :header-cell-style="{
            color: '#333',
            fontFamily: 'MicrosoftYaHeiUI',
            fontWeight: 500,
            fontSize: '16px',
          }"
          :fit="true"
          :data="tableData.slice((currentPage - 1) * pageSize,currentPage * pageSize)"
        >
          <el-table-column
            prop="mailId"
            sortable
            label="邮件ID"
          ></el-table-column>
          <el-table-column
            prop="describe"
            sortable
            label="邮件描述"
          ></el-table-column>
          <el-table-column
            prop="effectStartTime"
            sortable
            :formatter="formatDate"
            label="邮件生效时间"
          ></el-table-column>
          <el-table-column
            prop="effectEndTime"
            sortable
            :formatter="formatDate"
            label="邮件失效时间"
          ></el-table-column>
          <el-table-column prop="enabled" sortable label="邮件状态">
            <template slot-scope="scope">
              <span v-if="scope.row.enabled == false" style="color: red">已禁用</span>
              <span v-if="scope.row.enabled" style="color: green">已启用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                v-if="scope.row.enabled == false"
                @click="dialogEdit(scope.row)"
              >查看/编辑</el-button>
              <el-button
                size="mini"
                type="primary"
                v-if="scope.row.enabled"
                @click="showMail(scope.row)"
              >查看/编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                v-if="scope.row.enabled"
                @click="Disable(scope.row)"
              >禁用</el-button>
              <el-button
                size="mini"
                v-if="scope.row.enabled == false"
                type="success"
                @click="Enable(scope.row)"
              >启用</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pageNation
          :total="tableData.length"
          :page-size="pageSize"
          @pageChange="pageChange"
          :page_index="currentPage"
        />
      </el-main>
      <MailDialog
        :dialog="dialog"
        :MailForm="MailForm"
        @update="getfileMsg($event)"
      />
    </div>
  </div>
</template>
<script>
import MailDialog from "../../components/gmtool/MailDialog";
import pageNation from "../../components/pagenation/pageNation";
import { formatDate, getTime } from "../../utils/index";
export default {
  name: "Mail",
  components: { MailDialog, pageNation },
  data() {
    return {
      formatDate: formatDate,
      loading: false,
      MailForm: {
        effectStartTime: "",
        effectEndTime: "",
        userRegStartTime: "",
        userRegEndTime: "",
        describe: "",
        server_ids: "",
        award: [
          {
            goodsNum: "",
            goodsId: "",
          },
        ],
        uids: "",
        cnTitle: "",
        cnContent: "",
        enTitle: "",
        enContent: "",
        frenchTitle: "",
        frenchContent: "",
        germanTitle: "",
        germanContent: "",
        russianTitle: "",
        russianContent: "",
        userMinAfk: "",
        userMinLevel: "",
      },
      tableData: [],
      dialog: {
        show: false,
        title: "",
        option: "edit", //默认为编辑操作
        mailSearch: false,
      },
      mail: "",
      currentPage: 1,
      pageSize: 10,
    };
  },
  mounted() {
    this.getMailMsg();
  },
  methods: {
    //查看邮件信息
    showMail(row) {
      let efsTime = "";
      let efeTime = "";
      let ursTime = "";
      let ureTime = "";
      this.$http.post("/api/gmtool/getmailId", {
        mailId: row.mailId,
        user_actions: "查看邮件信息",
        date: getTime(new Date()),
      }).then((res) => {
          if (res.data.code == 200) {
            res.data.result.forEach((item) => {
              if (item.effectStartTime == 0) {
                efsTime = "";
              } else {
                efsTime = item.effectStartTime * 1000;
              }
              if (item.effectEndTime == 0) {
                efeTime = "";
              } else {
                efeTime = item.effectEndTime * 1000;
              }
              if (item.userRegStartTime == 0) {
                ursTime = "";
              } else {
                ursTime = item.userRegStartTime * 1000;
              }
              if (item.userRegEndTime == 0) {
                ureTime = "";
              } else {
                ureTime = item.userRegEndTime * 1000;
              }
              this.MailForm.mailId = item.mailId;
              this.MailForm.describe = item.describe;
              this.MailForm.effectStartTime = efsTime;
              this.MailForm.effectEndTime = efeTime;
              this.MailForm.userRegStartTime = ursTime;
              this.MailForm.userRegEndTime = ureTime;
              this.MailForm.uids = item.specifiedUids.toString();
              this.MailForm.server_ids = item.specifiedServerIds.toString();
              if (item.awards.length == 0) {
                this.MailForm.award = [
                  {
                    goodsNum: "",
                    goodsId: "",
                  },
                ];
              } else {
                this.MailForm.award = item.awards;
              }
              this.MailForm.userMinLevel = item.userMinLevel;
              this.MailForm.userMinAfk = item.userMinAfk;
              item.titles.forEach((valtitle, idx) => {
                if (idx == 0) {
                  this.MailForm.cnTitle = valtitle;
                } else if (idx == 2) {
                  this.MailForm.enTitle = valtitle;
                } else if (idx == 3) {
                  this.MailForm.frenchTitle = valtitle;
                } else if (idx == 4) {
                  this.MailForm.germanTitle = valtitle;
                } else if (idx == 5) {
                  this.MailForm.russianTitle = valtitle;
                }
              });
              item.bodies.forEach((valcontent, idx) => {
                if (idx == 0) {
                  this.MailForm.cnContent = valcontent;
                } else if (idx == 2) {
                  this.MailForm.enContent = valcontent;
                } else if (idx == 3) {
                  this.MailForm.frenchContent = valcontent;
                } else if (idx == 4) {
                  this.MailForm.germanContent = valcontent;
                } else if (idx == 5) {
                  this.MailForm.russianContent = valcontent;
                }
              });
            });
            this.dialog = {
              show: true,
              title: "查看邮件",
              option: "search",
              mailSearch: true,
            };
          }
        });
    },
    //禁用邮件
    Disable(row) {
      this.$confirm("确定禁用邮件吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http.post("/api/gmtool/disableMail", {
            mailId: row.mailId,
            user_actions: "禁用邮件",
            date: getTime(new Date()),
        }).then((res) => {
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);
              this.getMailMsg();
            }
        });
      }).catch(() => {
          return;
      });
    },
    //启用邮件
    Enable(row) {
      this.$confirm("确定启用邮件吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
          this.$http.post("/api/gmtool/enableMail", {
              mailId: row.mailId,
              user_actions: `启用邮件id为${row.mailId}邮件信息`,
              date: getTime(new Date()),
          }).then((res) => {
              this.$message.success(res.data.msg);
              this.getMailMsg();
          });
        }).catch(() => {
          return;
        });
    },
  //获取邮件信息
    getMailMsg() {
      this.loading = true;
      this.$http.get("/api/gmtool/getmail").then((res) => {
        if (res.data.code == 200) {
          this.tableData = res.data.result;
          this.loading = false;
        } else {
          this.$message({ type: "warning", message: "没有对应的数据" });
          this.loading = false;
        }
      });
    },
    //分页
    pageChange(item) {
      this.currentPage = item.page_index;
      this.pageSize = item.page_limit;
    },
    //新增邮件
    sendMail() {
        this.dialog = {
          show: true,
          title: "发送邮件",
          option: "add",
          mailSearch: false,
        };
        this.MailForm = {
          mailId: "",
          effectStartTime: "",
          effectEndTime: "",
          userRegStartTime: "",
          userRegEndTime: "",
          describe: "",
          award: [
            {
              goodsNum: "",
              goodsId: "",
            },
          ],
          uids: "",
          server_ids: "",
          cnTitle: "",
          cnContent: "",
          enTitle: "",
          enContent: "",
          frenchTitle: "",
          frenchContent: "",
          germanTitle: "",
          germanContent: "",
          russianTitle: "",
          russianContent: "",
          userMinAfk: "",
          userMinLevel: "",
        }
    },
    //获取mailId对应的字段信息
    dialogEdit(row) {
      let efsTime = "";
      let efeTime = "";
      let ursTime = "";
      let ureTime = "";
      this.$http.post("/api/gmtool/getmailId", {
        user_actions: "查看编辑邮件",
        date: getTime(new Date()),
        mailId: row.mailId,
      }).then((res) => {
          if (res.data.code == 200) {
            res.data.result.forEach((item) => {
              if (item.effectStartTime == 0) {
                efsTime = "";
              } else {
                efsTime = item.effectStartTime * 1000;
              }
              if (item.effectEndTime == 0) {
                efeTime = "";
              } else {
                efeTime = item.effectEndTime * 1000;
              }
              if (item.userRegStartTime == 0) {
                ursTime = "";
              } else {
                ursTime = item.userRegStartTime * 1000;
              }
              if (item.userRegEndTime == 0) {
                ureTime = "";
              } else {
                ureTime = item.userRegEndTime * 1000;
              }
              this.MailForm.mailId = item.mailId;
              this.MailForm.describe = item.describe;
              this.MailForm.effectStartTime = efsTime;
              this.MailForm.effectEndTime = efeTime;
              this.MailForm.userRegStartTime = ursTime;
              this.MailForm.userRegEndTime = ureTime;
              this.MailForm.uids = item.specifiedUids.toString();
              this.MailForm.server_ids = item.specifiedServerIds.toString();
              if (item.awards.length == 0) {
                this.MailForm.award = [
                  {
                    goodsNum: "",
                    goodsId: "",
                  },
                ];
              } else {
                this.MailForm.award = item.awards;
              }
              this.MailForm.userMinLevel = item.userMinLevel;
              this.MailForm.userMinAfk = item.userMinAfk;
              item.titles.forEach((valtitle, idx) => {
                if (idx == 0) {
                  this.MailForm.cnTitle = valtitle;
                } else if (idx == 2) {
                  this.MailForm.enTitle = valtitle;
                } else if (idx == 3) {
                  this.MailForm.frenchTitle = valtitle;
                } else if (idx == 4) {
                  this.MailForm.germanTitle = valtitle;
                } else if (idx == 5) {
                  this.MailForm.russianTitle = valtitle;
                }
              });
              item.bodies.forEach((valcontent, idx) => {
                if (idx == 0) {
                  this.MailForm.cnContent = valcontent;
                } else if (idx == 2) {
                  this.MailForm.enContent = valcontent;
                } else if (idx == 3) {
                  this.MailForm.frenchContent = valcontent;
                } else if (idx == 4) {
                  this.MailForm.germanContent = valcontent;
                } else if (idx == 5) {
                  this.MailForm.russianContent = valcontent;
                }
              });
            });
            this.dialog = {
              show: true,
              title: "编辑邮件",
              option: "edit",
              mailSearch: false,
            };
          }
        });
    },
    //编辑和添加邮件
    getfileMsg(mail) {
      var Title = [];
      var Content = [];
      Title.push(
        mail.cnTitle,
        "",
        mail.enTitle,
        mail.frenchTitle,
        mail.germanTitle,
        mail.russianTitle
      );
      Content.push(
        mail.cnContent,
        "",
        mail.enContent,
        mail.frenchContent,
        mail.germanContent,
        mail.russianContent
      );
      if(this.dialog.option == "add"){
           this.$http.post('/api/gmtool/addmail', {
              user_actions: "添加邮件",
              date: getTime(new Date()),
              mailId: mail.mailId,
              describe: mail.describe,
              awards: mail.award,
              specifiedServerIds: mail.specifiedServerIds,
              effectStartTime: mail.effectStartTime / 1000,
              effectEndTime: mail.effectEndTime / 1000,
              userRegStartTime: mail.userRegStartTime / 1000,
              userRegEndTime: mail.userRegEndTime / 1000,
              specifiedUids: mail.specifiedUid,
              title: Title,
              content: Content,
              userMinLevel: mail.userMinLevel,
              userMinAfk: mail.userMinAfk,
            }).then((res) => {
              if (res.data.code == 200) {
                this.$message.success(res.data.msg);
                this.getMailMsg();
              } else {
                this.$message({ type: "warning", message: res.data.msg });
              }
            });
      }else{
         this.$http.post(`/api/gmtool/updmail/${mail.mailId}`, {
              user_actions: "编辑邮件",
              date: getTime(new Date()),
              mailId: mail.mailId,
              describe: mail.describe,
              awards: mail.award,
              specifiedServerIds: mail.specifiedServerIds,
              effectStartTime: mail.effectStartTime / 1000,
              effectEndTime: mail.effectEndTime / 1000,
              userRegStartTime: mail.userRegStartTime / 1000,
              userRegEndTime: mail.userRegEndTime / 1000,
              specifiedUids: mail.specifiedUid,
              title: Title,
              content: Content,
              userMinLevel: mail.userMinLevel,
              userMinAfk: mail.userMinAfk,
            }).then((res) => {
              if (res.data.code == 200) {
                this.$message.success(res.data.msg);
                this.getMailMsg();
              } else {
                this.$message({ type: "warning", message: res.data.msg });
              }
        });
      }
    },
  },
};
</script>
<style scoped>
.mail {
  width: 100%;
  height: auto;
  background-color: #fff;
}
.mail td {
  padding: 18px;
  font-size: 14px;
  display: flex;
  float: left;
}
</style>